<?php
  $RUNSTATE = "undefined";
  abstract class Application {
    public $width = 1000;
    public $height = 600;
    public $bgcolor = "#ffffff";
    public $is_centered = true;
    public $objs = array();
    public $action_objs = array();

    abstract function init();
    function __construct() {
      global $RUNSTATE;
      $RUNSTATE = "init";
      $this->init();
      if(@$_REQUEST['action']) {
        $RUNSTATE = "javascript";
        $action = array();
        $action['action'] = $_REQUEST['action'];
        $action['component'] = @$_REQUEST['component'];
        foreach($this->objs as $obj) {
          if(isset($_REQUEST[$obj->uuid])) $obj->update($_REQUEST[$obj->uuid]);
        }
        foreach($this->action_objs as $obj) {
          if(strncmp($action['component'],$obj->uuid,strlen($obj->uuid)) == 0) $obj->actionPerformed($action);
        }
        $this->actionPerformed($action);
      } else {
        $RUNSTATE = "html";
        $this->print_header();
        $this->print_mainwindow();
        $this->print_bottom();
      }
    }
    function addActionListener($obj) {
       $this->action_objs[] = $obj; 
    }
    function actionPerformed($action) { 
    }
    function setLayout($layout) {
    }
    function add(&$obj) {
       $this->objs[] = $obj;
    }
    function print_mainwindow() {
      if($this->is_centered) echo "<center>";
      echo "<div id='mainwindow'>";
      echo "<script language=\"JavaScript\" type=\"text/JavaScript\">\n";
      echo "var F = new Object();\n";
      echo "function updatePOST() {\n";
      echo "  var str = '';\n";
      echo "  for (var i in F) {\n";
      echo "      func = eval('('+F[i]+')');\n";
      echo "      str = str + '&' + i + '=' + func();\n";
      echo "  }\n";
      echo "  return str;\n";
      echo "}\n";
      echo "function Remove(EId) {\n";
      echo "  return(EObj=document.getElementById(EId))?EObj.parentNode.removeChild(EObj):false;\n";
      echo "}\n";
      echo "function send_event(component,action,isolate) {\n";
      echo "var x;\n";
      echo "if (window.XMLHttpRequest) { x=new XMLHttpRequest(); }\n";
      echo "else { x=new ActiveXObject('Microsoft.XMLHTTP'); }\n";
      echo "x.onreadystatechange=function() {\n";
      echo "  if (x.readyState==4 && x.status==200) {\n";
      echo "    eval(x.responseText);\n";
      echo "  }\n";
      echo "}\n";
      echo "  url = '" . $_SERVER["SCRIPT_NAME"] . "?';\n";
      echo "  url = url + 'component=' + component + '&action=' + action;\n";
      echo "  if(isolate == false) url = url + updatePOST();\n";
      echo "x.open('GET',url,true);\n";
      echo "x.send();\n";
      echo "}\n";      
      echo "var dragok = false;\n";
      echo "var d = false;\n";
      echo "var sx = 0 ,sy = 0;\n";
      echo "document.onmousemove  = function(e){\n";
      echo "  if (!e) e = window.event;\n";
      echo "  if (dragok) if(d) {\n";
      echo "    x = e.clientX;\n";
      echo "    y = e.clientY;\n";      
      echo "    d.style.left = parseInt(d.style.left) + x - sx + 'px';\n";
      echo "    d.style.top  = parseInt(d.style.top) + y - sy + 'px';\n";
      echo "    sx = x;\n";
      echo "    sy = y;\n";
      echo "    return false;\n";
      echo "  }\n";
      echo "}\n";
      foreach($this->objs as $obj) {
         $obj->show("mainwindow");
      }
      echo "window.onload = function () {\n";
      echo "	 document.getElementById('mainwindow').attachEvent('onselectstart', disableFunction);\n";
      echo "	}\n";
      echo "	function disableFunction() {return false;} \n";

      echo "</script>\n";
      echo "</div>";
      if($this->is_centered) echo "</center>";
    }
    function print_header() {
      echo "<html>\n";
      echo "<header>\n";
      echo "<style type=\"text/css\">\n";
      echo "#mainwindow {\n";
      echo "  position: relative;\n";
      echo "  width: " . $this->width . "px;\n";
      echo "  height: " . $this->height . "px;\n";
      echo "  background: " . $this->bgcolor . ";\n";
      echo "  border: 1px solid #D9D9D9;\n";
      echo "  text-align: left;\n";

      echo "  -moz-user-select: -moz-none;\n";
      echo "  -khtml-user-select: none;\n";
      echo "  -webkit-user-select: none;\n";
      echo "  -ms-user-select: none;\n";
      echo "  user-select: none;\n";

      echo "}\n";
      echo "</style>\n";
      echo "</header>\n";
      echo "<body>\n";
    }
    function print_bottom() {
      echo "</body>\n";
      echo "</html>\n";
    }
  }


?>
